﻿CREATE PROCEDURE [acms].[Site_Put]
	@Id int, 
	@Name varchar(20),
	@Root varchar(255),
	@ConcurencyId rowversion,
	@IdOut int output,
	@ConcurencyIdOut rowversion output
AS

if exists (Select null from [Site_View] where Id = @Id) begin
	UPDATE Sites
	Set 
		Name = @Name,
		Root = @Root
	WHERE 
		Id= @Id
		AND ConcurencyId = @ConcurencyId
	
	if @@rowcount = 0
		return -100
	
	select @IdOut = Id, @ConcurencyIdOut = ConcurencyId from Sites where Id = @Id
end
else begin
	if @Id is null or @Id <= 0 begin
		INSERT Sites
			(Name, Root)
		Values
			(@Name, @Root)
		set @IdOut = @@identity
		
		select @ConcurencyIdOut = ConcurencyId from Sites where Id = @IdOut
	end
	else begin
		return -200
	end

end